package hrms.controller;

import hrms.entity.Candidate;
import hrms.entity.Recruitment;
import hrms.entity.Result;
import hrms.entity.Staff;
import hrms.service.AccountService;
import hrms.service.RecruitmentService;
import hrms.service.StaffService;
import hrms.util.JWTUtil;
import hrms.util.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/recruitment")
public class RecruitmentController {
    @Autowired
    private RecruitmentService recruitmentService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private StaffService staffService;

    public Staff getStaff(HttpServletRequest request){
        String token = request.getHeader("token");
        String id = JWTUtil.getId(token);
        Integer staffId = accountService.findStaffIdById(Integer.valueOf(id));
        return staffService.findById(staffId);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Map<String, Object> map = new HashMap<>();
        map.put("recruitment", recruitmentService.findById(id));
        return Result.success().addMap(map);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<Recruitment> recruitments = recruitmentService.findAll();
        if(recruitments.size()==0 || recruitments == null){
            return Result.fail().error("没有招聘信息");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("recruitmentList", recruitments);
        return Result.success().addMap(map);
    }

    @PostMapping("/insertRecruitment")
    public Result insertRecruitment(@RequestBody Recruitment recruitment, HttpServletRequest request){
        recruitment.setStaff(getStaff(request));
        recruitmentService.insertRecruitment(recruitment);
        return Result.success();
    }

    @GetMapping(value="/findPage")
    public Object findPage(@RequestParam(defaultValue = "1") int page){
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(5);
        return recruitmentService.findPage(pageRequest);
    }

    @GetMapping("/findStatus0")
    public Result findStatus0(){
        List<Recruitment> recruitments = recruitmentService.findStatus0();
        Map<String, Object> map = new HashMap<>();
        map.put("recruitments", recruitments);
        return Result.success().addMap(map);
    }

    @PostMapping(value="/updateQuantity")
    public Result updateQuantity(@RequestBody Recruitment recruitment){
        recruitmentService.updateQuantity(recruitment);
        return Result.success();
    }

    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody Recruitment recruitment){
        recruitmentService.updateStatus(recruitment);
        return Result.success();
    }

}
